package com.example.security.handler;

import com.example.security.common.CommonResult;
import com.example.security.exceptions.UnavailableException;
import com.example.security.exceptions.UserNotFoundException;
import com.example.security.exceptions.VerifyFailedException;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理器
 * <p>
 * Created by devinlee in 2022.09.09
 */
@Log4j2
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(UserNotFoundException.class)
    public CommonResult exceptionHandler(HttpServletRequest request, UserNotFoundException exception) {
        
        log.error(exception.getMessage() + request.getRequestURL().toString());
        return CommonResult.failed(exception.getMessage());
    }
    
    @ExceptionHandler(UnavailableException.class)
    public CommonResult exceptionHandler(HttpServletRequest request, UnavailableException exception) {
        
        log.error(exception.getMessage() + request.getRequestURL().toString());
        return CommonResult.failed(exception.getMessage());
    }
    
    @ExceptionHandler(VerifyFailedException.class)
    public CommonResult exceptionHandler(HttpServletRequest request, VerifyFailedException exception) {
        
        log.error(exception.getMessage() + request.getRequestURL().toString());
        return CommonResult.failed(exception.getMessage());
    }
}
